package com.nkzy.mapper;

import com.nkzy.pojo.Address;
import org.apache.ibatis.annotations.*;

import java.util.List;
@Mapper
public interface AddressMapper {

    /**
     * 插入新的地址信息
     *
     * @param address 要插入的地址对象
     * @return 影响的行数
     */
    @Insert("INSERT INTO address (id, user_id, province, city, town, mobile, street, contact, is_default, notes, deleted) " +
            "VALUES (#{id}, #{userId}, #{province}, #{city}, #{town}, #{mobile}, #{street}, #{contact}, #{isDefault}, #{notes}, #{deleted})")
    int insert(Address address);

    /**
     * 根据ID删除地址信息
     *
     * @param id 要删除的地址ID
     * @return 影响的行数
     */
    @Delete("DELETE FROM address WHERE id = #{id}")
    int deleteById(Long id);

    /**
     * 更新地址信息
     *
     * @param address 要更新的地址对象
     * @return 影响的行数
     */
    @Update("UPDATE address SET user_id = #{userId}, province = #{province}, city = #{city}, town = #{town}, " +
            "mobile = #{mobile}, street = #{street}, contact = #{contact}, is_default = #{isDefault}, notes = #{notes}, deleted = #{deleted} " +
            "WHERE id = #{id}")
    int update(Address address);

    /**
     * 根据ID查询地址信息
     *
     * @param id 要查询的地址ID
     * @return 查询到的地址对象
     */
    @Select("SELECT * FROM address WHERE id = #{id}")
    Address selectById(Long id);

    /**
     * 查询所有地址信息
     *
     * @return 地址列表
     */
    @Select("SELECT * FROM address")
    List<Address> selectAll();

    /**
     * 根据用户ID查询地址信息
     *
     * @param userId 用户ID
     * @return 地址列表
     */
    @Select("SELECT * FROM address WHERE user_id = #{userId}")
    List<Address> selectByUserId(Long userId);

    /**
     * 根据多个ID查询地址信息
     *
     * @param ids 要查询的地址ID列表
     * @return 地址列表
     */
    @Select("<script>" +
            "SELECT * FROM address WHERE id IN " +
            "<foreach collection='list' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    List<Address> selectByIds(List<Long> ids);

    /**
     * 查询默认地址
     *
     * @param userId 用户ID
     * @return 默认地址对象
     */
    @Select("SELECT * FROM address WHERE user_id = #{userId} AND is_default = 1 LIMIT 1")
    Address selectDefaultAddress(Long userId);
}